; This module initializes the microcontroller then executes MAIN forever
;
Reset:
MOV SP, #235 ; Initialize the Stack
MOV PageReg, #7FH ; Allows MOVX Ri to access EZ-USB memory
MOV R0, #Low(USBControl) ; Simulate a disconnect
MOVX A, @R0
ANL A, #11110011b ; Clear DISCON, DISCOE
MOVX @R0, A
CALL Wait100msec ; Give the host time to react
MOVX A, @R0 ; Reconnect with this new identity
ORL A, #00000110b ; Set DISCOE to enable pullup resistor
MOVX @R0, A ; Set RENUM so that 8051 handles USB requests
CLR A
MOV FLAGS, A ; Start in Default state
TurnOffLEDs:
MOV LEDValue, A
MOV Old_Buttons, A
INC A ; = 1
MOV LEDstrobe, A
Initialize4msecCounter:
MOV Expired_Time, A
MOV Msec_counter, A
InitializeIOSystem: ; Work around the Dscope monitor I/O needs
; Setup Port A as 8-bit OUTPUT port (no alternate functions)
; Setup an 8-bit INPUT port using PortB_Bits [7:4] and PortC_Bits[3:0]
; Assume a pre-existing configuration (ie Dscope)
MOV R0, #LOW(PortA_Config) ; PageReg = 7F = HIGH(PortA_Config)
CLR A
MOVX @R0, A ; No alternate functions
MOV R1, #LOW(PortA_OE)
CPL A ; = 0FFH
MOVX @R1, A ; Enable PortA for Output
INC R0 ; Point to PortB_Config
INC R1 ; Point to PortB_OE
MOVX A, @R0 ; Get current configuration
ANL A, #0FH
MOVX @R0, A ; No alternate functions on upper nibble
MOVX A, @R1 ; Get current configuration
ORL A, #0F0H
MOVX @R1, A ; Enable PortB_Bits[7:4] for Output
INC R0 ; Point to PortC_Config
INC R1 ; Point to PortC_OE
MOVX A, @R0 ; Get current configuration
ANL A, #0F0H
MOVX @R0, A ; No alternate functions on lower nibble
MOVX A, @R1 ; Get current configuration
ORL A, #0FH
MOVX @R1, A ; Enable PortC_Bits[3:0] for Output
InitializeInterruptSystem: ; First initialize the USB level
CLR A
MOV R0, #LOW(IN07IEN)
MOVX @R0, A ; Disable interrupts from IN Endpoints 0-7
INC R0
MOVX @R0, A ; Disable interrupts from OUT Endpoints 0-7
INC R0
MOV A, #00000011b
MOVX @R0, A ; Enable (Resume, Suspend,) SOF and SUDAV INTs
INC R0
MOV A, #00000001b
MOVX @R0, A ; Enable Auto Vectoring for USB interrupts
; Now enable the main level
MOV EIE, #00000001b ; Enable INT2 = USB Interrupt (only)
MOV EI, #11000000b ; Enable interrupt subsystem (and Ser1 for dScope)
; Initialization Complete.
;
MAIN:
NOP ; Not much of a main loop for this example
JMP MAIN ; All actions are initiated by interrupts
; We are a slave, we wait to be told what to do
Wait100msec:
MOV Temp, #100
Wait1msec: ; A delay loop
MOV DPTR, #-1200
More: INC DPTR ; 3 cycles
MOV A, DPL ; + 2
ORL A, DPH ; + 2
JNZ More ; + 3 = 10 cycles x 1200 = 1msec
DJNZ Temp, Wait1msec
RET
ProcessOutputReport: ; A Report has just been received
; The report is only one byte long in this first example
; It contains a new value for the LEDs
MOV DPTR, #EP0OutBuffer ; Point to the Report
MOVX A, @DPTR ; Get the Data
MOV LEDValue, A ; Update the local variable
RET
CreateInputReport: ; Called from TIMER which detected the need
; The report is only one byte long in this first example
; It contains a new value for the Buttons
MOV DPTR, #EP1InBuffer ; Point to the buffer
MOVX @DPTR, A ; Update the Report
MOV DPTR, #IN1ByteCount
MOV A, #1
MOVX @DPTR, A ; Endpoint 1 now 'armed', next IN will get data
RET